package com.jc.oss.service;

import com.jc.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author gongjiawei
 * @Date 2024/9/13 11:43
 * @注释
 */
@Service
public class FileService {

    /*
    * 构造器注入
    * */

    public final StorageAdapter STORAGE_SERVICE;

    public FileService(StorageAdapter storageAdapter){
        this.STORAGE_SERVICE= storageAdapter;
    }
    /*
     * 列出所有桶
     *
     * */
    public List<String> getAllBucket(){
        return STORAGE_SERVICE.getAllBucket();
    }
    /**
     * 获取文件路径
     */
    public String getUrl(String bucketName,String objectName) {
        return STORAGE_SERVICE.getUrl(bucketName,objectName);
    }

    /**
     * 上传文件
     */
    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName){
        STORAGE_SERVICE.uploadFile(uploadFile,bucket,objectName);
        objectName = objectName + "/" + uploadFile.getOriginalFilename();
        return STORAGE_SERVICE.getUrl(bucket, objectName);
    }
}
